package com.example.exam.service;

import com.example.exam.exception.ServiceException;
import com.example.exam.mapper.BookTypeMapper;
import com.example.exam.pojo.BookTypePage;
import com.example.exam.pojo.Booktype;
import com.example.exam.service.imp.BookTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class MyBookTypeService implements BookTypeService {

    @Autowired
    BookTypeMapper bookTypeMapper;

    @Override
    public void insertBookType(Booktype booktype) {
        int i=bookTypeMapper.insertBookType(booktype);
        if(i==0){
            throw  new ServiceException("书本分类插入失败");
        }
    }

    @Override
    public void updateBookType(Booktype booktype) {
        int i=bookTypeMapper.updateBookType(booktype);
        if(i==0){
            throw  new ServiceException("书本分类更新失败");
        }
    }

    @Override
    public BookTypePage selectBookTypeByLimit(Long row, Long number) {
        long count = bookTypeMapper.countRow();
        long rows = count/number;
        if(count%number!=0){
            rows++;
        }
        BookTypePage bookTypePage = new BookTypePage();
        bookTypePage.setBooktypes(bookTypeMapper.selectBookTypeByLimit((row-1)*number,number));
        bookTypePage.setCount(count);
        bookTypePage.setRows(rows);
        return bookTypePage;
    }

    @Override
    public void deleteBookType(Long id) {
        int i=bookTypeMapper.deleteBookType(id);
        if(i==0){
            throw  new ServiceException("书本分类删除失败");
        }
    }
}
